#include <iostream>
#include <cmath>
using namespace std;

const int N = 15;
int a[N], c[N];
int ret = 1e9;
int n;

void dfs(int s, int b, int step)
{
    if (step == n)
    {
        if (s == 1 && b == 0) return;
        ret = min(ret, abs(s - b));
        return;
    }

    dfs(s * a[step], b + c[step], step + 1);
    dfs(s, b, step + 1);
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i] >> c[i];

    dfs(1, 0, 0);
    cout << ret << endl;

    return 0;
}
